package com.rongke.web.controller.medical;

import com.alibaba.fastjson.JSONObject;
import com.rongke.commons.JsonResp;
import com.rongke.model.ReportRecord;
import com.rongke.service.ReportRecordService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 医生诊断书
 * 
 * @ReportRecordController
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value = "/api/reportRecord")
@Transactional
public class ReportRecordController {
	private Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private ReportRecordService reportRecordService;

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public JsonResp addReportRecord(@RequestBody ReportRecord reportRecord) {

		try {
			log.debug("addReportRecord");

			reportRecordService.insert(reportRecord);
			JSONObject json = new JSONObject();
			json.put("id", reportRecord.getId());

			return JsonResp.ok(json);
		} catch (Exception e) {
			log.error("addReportRecord", e);
			return JsonResp.fa(null);
		}
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public JsonResp updateReportRecord(@RequestBody ReportRecord reportRecord) {
		log.debug("updateReportRecord");

		try {
			reportRecord.setGmtCreate(null);// 不修改创建时间
			reportRecord.setGmtModified(new Date());
			reportRecord.setPicContent(null);
			reportRecord.setUserId(null);
			reportRecord.setMedicalRecordId(null);

			reportRecordService.updateById(reportRecord);

			JSONObject json = new JSONObject();
			json.put("id", reportRecord.getId());
			return JsonResp.ok(json);
		} catch (Exception e) {
			log.error("updateInspectionRecord failed", e);
			return JsonResp.fa(null);

		}
	}

	/**
	 * 根据ID删除检查记录
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public JsonResp delReportRecordById(long id) {
		if (log.isInfoEnabled()) {
			log.info("delReportRecordById, id=" + id);
		}

		try {
			reportRecordService.deleteById(id);

			JSONObject json = new JSONObject();
			json.put("id", id);
			return JsonResp.ok(json);
		} catch (Exception e) {
			log.error("delReportRecordById failed, id=" + id, e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * @根据id查找病历记录报告
	 * @param id
	 * @return 返回值JsonResp
	 */
	@RequestMapping(value = "/selectOne", method = RequestMethod.GET)
	public JsonResp selectReportRecord(Long id) {
		log.debug("selectReportRecord");

		try {
			ReportRecord reportRecord = reportRecordService.selectById(id);
			return JsonResp.ok(reportRecord);
		} catch (Exception e) {
			log.error("selectReportRecord failed, id=" + id, e);
			return JsonResp.fa(null);
		}
	}

	/**
	 * 根据具体的病历，获取所有的医生诊断书
	 * 
	 * @param medicalRecordId
	 * @return
	 */
	@RequestMapping(value = "/selectAll", method = RequestMethod.GET)
	public JsonResp selectList(long medicalRecordId) {
		if (log.isInfoEnabled()) {
			log.info("selectList, medicalRecordId=" + medicalRecordId);
		}

		try {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("medical_record_id", medicalRecordId);
			List<ReportRecord> result = reportRecordService.selectByMap(params);
			return JsonResp.ok(result);
		} catch (Exception e) {
			log.error("selectList failed, medicalRecordId=" + medicalRecordId,
					e);
			return JsonResp.fa(null);
		}
	}
}
